home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / WWW / swish.11 / src / file.c < prev    next >
C/C++ Source or Header  |  1995-03-11  |  12KB  |  310 lines

  1. /*
  2. ** Copyright (C) 1995, Enterprise Integration Technologies Corp.        
  3. ** All Rights Reserved.
  4. ** Kevin Hughes, kevinh@eit.com 
  5. ** 3/11/94
  6. */
  7.  
  8. #include "swish.h"
  9. #include "file.h"
  10.  
  11. /* Is a file a directory?
  12. */
  13.  
  14. int isdirectory(path)
  15.      char *path;
  16. {
  17.         struct stat stbuf;
  18.  
  19.         if (stat(path, &stbuf))
  20.                 return 0;
  21.         return ((stbuf.st_mode & S_IFMT) == S_IFDIR) ? 1 : 0;
  22. }
  23.  
  24. /* Is a file a regular file?
  25. */
  26.  
  27. int isfile(path)
  28.      char *path;
  29. {
  30.         struct stat stbuf;
  31.  
  32.         if (stat(path, &stbuf))
  33.                 return 0;
  34.         return ((stbuf.st_mode & S_IFMT) == S_IFREG) ? 1 : 0;
  35. }
  36.  
  37. /* Is a file a link?
  38. */
  39.  
  40. int islink(path)
  41. char *path;
  42. {
  43.         struct stat stbuf;
  44.  
  45.         if (lstat(path, &stbuf))
  46.                 return 0;
  47.         return ((stbuf.st_mode & S_IFLNK) == S_IFLNK) ? 1 : 0;
  48. }
  49.  
  50. /* Get the size, in bytes, of a file.
  51. ** Return -1 if there's a problem.
  52. */
  53.  
  54. int getsize(path)
  55.      char *path;
  56. {
  57.         struct stat stbuf;
  58.  
  59.         if (stat(path, &stbuf))
  60.                 return -1;
  61.         return stbuf.st_size;
  62. }
  63.  
  64. /* Reads the configuration file and puts all the right options
  65. ** in the right variables and structures.
  66. */
  67.  
  68. void getdefaults(conffile, hasdir, hasindex, plimit, flimit, hasverbose)
  69.      char *conffile;
  70.      int *hasdir;
  71.      int *hasindex;
  72.      int *plimit;
  73.      int *flimit;
  74.      int hasverbose;
  75. {
  76.         int skiplen, gotdir, gotindex;
  77.         char *c, line[MAXSTRLEN], value[MAXSTRLEN];
  78.         FILE *fp;
  79.  
  80.     gotdir = gotindex = 0;
  81.  
  82.         if ((fp = fopen(conffile, "r")) == NULL) {
  83.         sprintf(errorstr,
  84.         "Couldn't open the configuration file \"%s\".", conffile);
  85.                 progerr(errorstr);
  86.     }
  87.         while (fgets(line, MAXSTRLEN, fp) != NULL) {
  88.                 if (line[0] == '#' || line[0] == '\n')
  89.                         continue;
  90.                 if (c = (char *) lstrstr(line, "IndexDir")) {
  91.                         c += strlen("IndexDir");
  92.                         while (!*hasdir) {
  93.                                 strcpy(value, (char *) getword(c, &skiplen));
  94.                                 if (!skiplen | value[0] == '\0' ||
  95.                                 value[0] == '\n')
  96.                                         break;
  97.                                 else {
  98.                                         c += skiplen;
  99.                                         dirlist = (struct swline *)
  100.                                         addswline(dirlist, value);
  101.                     gotdir = 1;
  102.                                 }
  103.                         }
  104.                 }
  105.                 else if (c = (char *) lstrstr(line, "IndexFile")) {
  106.                         c += strlen("IndexFile");
  107.                         while (!*hasindex) {
  108.                                 strcpy(value, (char *) getword(c, &skiplen));
  109.                                 if (!skiplen | value[0] == '\0' ||
  110.                                 value[0] == '\n')
  111.                                         break;
  112.                                 else {
  113.                                         c += skiplen;
  114.                                         indexlist = (struct swline *)
  115.                                         addswline(indexlist, value);
  116.                     gotindex = 1;
  117.                                 }
  118.                         }
  119.                 }
  120. /* IndexVerbose is supported for backwards compatibility */
  121.         else if (c = (char *) lstrstr(line, "IndexVerbose")) {
  122.                         c += strlen("IndexVerbose");
  123.                         strcpy(value, (char *) getword(c, &skiplen));
  124.             verbose = (lstrstr(value, "yes")) ? 3 : 0;
  125.         }
  126.         else if (c = (char *) lstrstr(line, "IndexReport")) {
  127.                         c += strlen("IndexReport");
  128.                         strcpy(value, (char *) getword(c, &skiplen));
  129.                         if (!skiplen | value[0] == '\0' || value[0] == '\n')
  130.                                 continue;
  131.                         else {
  132.                                 c += skiplen;
  133.                 if (!hasverbose)
  134.                     verbose = atoi(value);
  135.                         }
  136.         }
  137.                 else if (c = (char *) lstrstr(line, "IndexOnly")) {
  138.                         c += strlen("IndexOnly");
  139.                         while (1) {
  140.                                 strcpy(value, (char *) getword(c, &skiplen));
  141.                                 if (!skiplen | value[0] == '\0' ||
  142.                                 value[0] == '\n')
  143.                                         break;
  144.                                 else {
  145.                                         c += skiplen;
  146.                                         suffixlist = (struct swline *)
  147.                                         addswline(suffixlist, value);
  148.                                 }
  149.                         }
  150.                 }
  151.                 else if (c = (char *) lstrstr(line, "NoContents")) {
  152.                         c += strlen("NoContents");
  153.                         while (1) {
  154.                                 strcpy(value, (char *) getword(c, &skiplen));
  155.                                 if (!skiplen | value[0] == '\0' ||
  156.                                 value[0] == '\n')
  157.                                         break;
  158.                                 else {
  159.                                         c += skiplen;
  160.                                         nocontentslist = (struct swline *)
  161.                                         addswline(nocontentslist, value);
  162.                                 }
  163.                         }
  164.                 }
  165.                 else if ((c = (char *) lstrstr(line, "pathname contains")) &&
  166.         (char *) lstrstr(line, "FileRules")) {
  167.                         c += strlen("pathname contains");
  168.                         while (1) {
  169.                                 strcpy(value, (char *) getword(c, &skiplen));
  170.                                 if (!skiplen | value[0] == '\0' ||
  171.                                 value[0] == '\n')
  172.                                         break;
  173.                                 else {
  174.                                         c += skiplen;
  175.                                         pathconlist = (struct swline *)
  176.                                         addswline(pathconlist, value);
  177.                                 }
  178.                         }
  179.                 }
  180.                 else if ((c = (char *) lstrstr(line, "directory contains")) &&
  181.         (char *) lstrstr(line, "FileRules")) {
  182.                         c += strlen("directory contains");
  183.                         while (1) {
  184.                                 strcpy(value, (char *) getword(c, &skiplen));
  185.                                 if (!skiplen | value[0] == '\0' ||
  186.                                 value[0] == '\n')
  187.                                         break;
  188.                                 else {
  189.                                         c += skiplen;
  190.                                         dirconlist = (struct swline *)
  191.                                         addswline(dirconlist, value);
  192.                                 }
  193.                         }
  194.                 }
  195.                 else if ((c = (char *) lstrstr(line, "filename contains")) &&
  196.         (char *) lstrstr(line, "FileRules")) {
  197.                         c += strlen("filename contains");
  198.                         while (1) {
  199.                                 strcpy(value, (char *) getword(c, &skiplen));
  200.                                 if (!skiplen | value[0] == '\0' ||
  201.                                 value[0] == '\n')
  202.                                         break;
  203.                                 else {
  204.                                         c += skiplen;
  205.                                         fileconlist = (struct swline *)
  206.                                         addswline(fileconlist, value);
  207.                                 }
  208.                         }
  209.                 }
  210.                 else if ((c = (char *) lstrstr(line, "title contains")) &&
  211.         (char *) lstrstr(line, "FileRules")) {
  212.                         c += strlen("title contains");
  213.                         while (1) {
  214.                                 strcpy(value, (char *) getword(c, &skiplen));
  215.                                 if (!skiplen | value[0] == '\0' ||
  216.                                 value[0] == '\n')
  217.                                         break;
  218.                                 else {
  219.                                         c += skiplen;
  220.                                         titconlist = (struct swline *)
  221.                                         addswline(titconlist, value);
  222.                                 }
  223.                         }
  224.                 }
  225.                 else if ((c = (char *) lstrstr(line, "filename is")) &&
  226.         (char *) lstrstr(line, "FileRules")) {
  227.                         c += strlen("filename is");
  228.                         while (1) {
  229.                                 strcpy(value, (char *) getword(c, &skiplen));
  230.                                 if (!skiplen | value[0] == '\0' ||
  231.                                 value[0] == '\n')
  232.                                         break;
  233.                                 else {
  234.                                         c += skiplen;
  235.                                         fileislist = (struct swline *)
  236.                                         addswline(fileislist, value);
  237.                                 }
  238.                         }
  239.                 }
  240.                 else if (c = (char *) lstrstr(line, "IgnoreWords")) {
  241.                         c += strlen("IgnoreWords");
  242.                         while (1) {
  243.                                 strcpy(value, (char *) getword(c, &skiplen));
  244.                                 if (!skiplen | value[0] == '\0' ||
  245.                                 value[0] == '\n')
  246.                                         break;
  247.                                 else {
  248.                                         c += skiplen;
  249.                     if (lstrstr(value, "SwishDefault"))
  250.                         readdefaultstopwords();
  251.                     else
  252.                         addstophash(value);
  253.                                 }
  254.                         }
  255.                 }
  256.                 else if ((c = (char *) lstrstr(line, "IgnoreLimit"))) {
  257.                         c += strlen("IgnoreLimit");
  258.                         strcpy(value, (char *) getword(c, &skiplen));
  259.                         if (!skiplen | value[0] == '\0' || value[0] == '\n')
  260.                                 continue;
  261.                         else {
  262.                                 c += skiplen;
  263.                 *plimit = atoi(value);
  264.                         }
  265.                         strcpy(value, (char *) getword(c, &skiplen));
  266.                         if (!skiplen | value[0] == '\0' || value[0] == '\n')
  267.                                 continue;
  268.                         else {
  269.                                 c += skiplen;
  270.                 *flimit = atoi(value);
  271.                         }
  272.                 }
  273.                 else if (c = (char *) lstrstr(line, "ReplaceRules")) {
  274.                         c += strlen("ReplaceRules");
  275.                         while (1) {
  276.                                 strcpy(value, (char *) getword(c, &skiplen));
  277.                                 if (!skiplen | value[0] == '\0' ||
  278.                                 value[0] == '\n')
  279.                                         break;
  280.                                 else {
  281.                                         c += skiplen;
  282.                                         replacelist = (struct swline *)
  283.                                         addswline(replacelist, value);
  284.                                 }
  285.                         }
  286.                 }
  287.                 else if ((char *) getconfvalue(line, "FollowSymLinks",
  288.         value) != NULL)
  289.                         followsymlinks = (lstrstr(value, "yes")) ? 1 : 0;
  290.                 else if ((char *) getconfvalue(line, "IndexName",
  291.         value) != NULL)
  292.                         strcpy(indexn, value);
  293.                 else if ((char *) getconfvalue(line, "IndexDescription",
  294.         value) != NULL)
  295.                         strcpy(indexd, value);
  296.                 else if ((char *) getconfvalue(line, "IndexPointer",
  297.         value) != NULL)
  298.                         strcpy(indexp, value);
  299.                 else if ((char *) getconfvalue(line, "IndexAdmin",
  300.         value) != NULL)
  301.                         strcpy(indexa, value);
  302.         }
  303.         fclose(fp);
  304.  
  305.     if (gotdir && !(*hasdir))
  306.         *hasdir = 1;
  307.     if (gotindex && !(*hasindex))
  308.         *hasindex = 1;
  309. }
  310.